#pragma once

#include <string>
#include <iostream>
#include "Protocol.hpp"

enum{
    Div_Zero = 1,
    Mod_Zero,
    Other_Oper
};

class SerCal
{
public:
    SerCal() {}
    ~SerCal() {}

    Response calculatorHelper(const Request &req)
    {
        Response ret(0, 0);

        switch (req.op)
        {
        case '+':
            ret.result = req.x + req.y;
            break;
        case '-':
            ret.result = req.x - req.y;
            break;
        case '*':
            ret.result = req.x * req.y;
            break;
        case '/':
        {
            if(req.y == 0)
                ret.status = Div_Zero;
            else
                ret.result = req.x / req.y;
            break;
        }
        case '%':
        {
            if(req.y == 0)
                ret.status = Mod_Zero;
            else
                ret.result = req.x % req.y;
            break; 
        }       
        default:
            ret.status = Other_Oper;
            break;
        }

        return ret;
    }

    std::string calculator(std::string &package)
    {
        std::string content;
        bool r = Decode(package, &content);
        if (!r)
            return "";

        Request req;

        r = req.Deserialize(content);
        if (!r)
            return "";

        content = "";
        Response rsp = calculatorHelper(req);
        rsp.Serialize(&content);
        content = Encode(content);

        return content;
    }
};